package com.rzx.mybackend.controller.game;

import com.rzx.mybackend.common.PageResult;
import com.rzx.mybackend.common.Result;
import com.rzx.mybackend.dto.BuyGamePageDTO;
import com.rzx.mybackend.entity.BuyGames;
import com.rzx.mybackend.service.BuyGameService;
import com.rzx.mybackend.service.UserService;
import com.rzx.mybackend.vo.BuyGamesVO;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * BuyGameController
 *
 * @author renzhexian
 * @date 2025/7/22
 */
@RestController
@RequestMapping("/buygame")
@RequiredArgsConstructor
public class BuyGameController {

    private final BuyGameService buyGameService;

    private final UserService userService;

    private static final Logger log = LoggerFactory.getLogger(BuyGameController.class);

    /**
     * 购买游戏
     *
     * @param buyGames
     * @return
     */
    @PostMapping
    public Result bugGames(@RequestBody BuyGames buyGames) {

        log.info("开始购买游戏");

        // 如果前端无法传递用户信息，可以通过SecurityContextHolder获取当前登录用户的信息
        Long userId = userService.selectUserIdByUserName(SecurityContextHolder.getContext().getAuthentication().getName());

        buyGames.setUserId(userId);

        buyGameService.buyGames(buyGames);

        return Result.success();
    }

    /**
     * 分页查询当前用户购买游戏列表
     *
     * @param buyGamePageDTO
     * @return
     */
    @GetMapping("/list")
    public PageResult<BuyGamesVO> pageBuyGameList(BuyGamePageDTO buyGamePageDTO) {

        log.info("开始分页查询购买游戏列表");

        Long userId = userService.selectUserIdByUserName(SecurityContextHolder.getContext().getAuthentication().getName());

        buyGamePageDTO.setUserId(userId);

        // 分页方法
        return buyGameService.pageBuyGameList(buyGamePageDTO);
    }
}
